Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Enable right click to paste in terminal #34

Merged
merged 8 commits into from
Aug 15, 2024
Merged

UI: Enable right click to paste in terminal #34

merged 8 commits into from
Aug 15, 2024

Conversation

SmartManoj
Copy link
Owner

@SmartManoj
Copy link
Owner Author

@CodiumAI-Agent /review

@CodiumAI-Agent
Copy link

PR Reviewer Guide 🔍

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Key issues to review

Error Handling
The new event listener for 'contextmenu' lacks error handling for the clipboard read operation. Consider adding a catch block for potential failures of navigator.clipboard.readText().

Performance Concern
Adding an event listener inside a hook without cleanup could lead to memory leaks or multiple instances of the same listener if the component re-renders. Ensure to remove the event listener in a cleanup function.

Logic Error
The condition if (!cleanedCommand) return; prevents sending empty commands, which might be necessary in some cases (e.g., to signal a stop). Verify if this change in logic aligns with the intended use cases.

@SmartManoj
Copy link
Owner Author

@CodiumAI-Agent /ask

Adding an event listener inside a hook without cleanup could lead to memory leaks or multiple instances of the same listener if the component re-renders. Ensure to remove the event listener in a cleanup function.

attachCustomKeyEventHandler also there.

@CodiumAI-Agent
Copy link

Ask

Adding an event listener inside a hook without cleanup could lead to memory leaks or multiple instances of the same listener if the component re-renders. Ensure to remove the event listener in a cleanup function. attachCustomKeyEventHandler also there.

Answer:

You are correct. Adding an event listener inside a hook without proper cleanup can lead to memory leaks or multiple instances of the same listener if the component re-renders. To address this, we should ensure that the event listener is removed when the component unmounts or when the hook is re-invoked. Here is an updated version of the useTerminal hook with the necessary cleanup:

import { useEffect, useRef } from 'react';

export const useTerminal = (commands: Command[] = []) => {
  const terminal = useRef<any>(null);
  const fitAddon = useRef<any>(null);
  let commandBuffer = '';

  useEffect(() => {
    const handleContextMenu = (e: MouseEvent) => {
      e.preventDefault();
      navigator.clipboard.readText().then((text) => {
        terminal.current?.write(text);
        commandBuffer += text;
      });
    };

    const terminalElement = terminal.current?.element;
    if (terminalElement) {
      terminalElement.addEventListener('contextmenu', handleContextMenu);
    }

    return () => {
      if (terminalElement) {
        terminalElement.removeEventListener('contextmenu', handleContextMenu);
      }
    };
  }, [terminal]);

  // Other hook logic...

  return {
    // Return values...
  };
};

In this updated version, we define the handleContextMenu function inside the useEffect hook and add it as an event listener to the terminal element. We also ensure to remove the event listener in the cleanup function returned by useEffect. This will prevent memory leaks and ensure that only one instance of the event listener is active at any time.

@SmartManoj SmartManoj merged commit 6cd29a5 into kevin Aug 15, 2024
11 checks passed
@SmartManoj SmartManoj deleted the terminal2 branch August 15, 2024 10:19
@SmartManoj SmartManoj restored the terminal2 branch August 15, 2024 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants